package com.features;

import java.net.InetAddress;

import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Who;

import com.utils.AbstractTrace;

/** <p>Title: 操作系统信息</p>
 * <p>Description: 获取操作系统相关信息</p>
 * <p>Copyright: Copyright (c) 2011</p>
 * @author zhangzuoqiang
 * @version 
 * <br/>Date: 2011-5-10
 */
public class PlatCommand extends AbstractTrace {
	
	/**
	 * 取到当前操作系统的名称
	 * @return
	 */
	public String getPlatformName() {
		String hostname = "";
		try {
			hostname = InetAddress.getLocalHost().getHostName();
		} catch (Exception exc) {
			Sigar sigar = new Sigar();
			try {
				hostname = sigar.getNetInfo().getHostName();
			} catch (SigarException e) {
				hostname = "localhost.unknown";
			} finally {
				sigar.close();
			}
		}
        return hostname;
	}
	
	/**
	 * 取当前操作系统的信息
	 * @return
	 */
	public OperatingSystem getGetOSInfo() {
		OperatingSystem os = OperatingSystem.getInstance();
		
		// 操作系统内核类型如： 386、486、586等x86
        System.out.println("OS.getArch() = " + os.getArch());
        System.out.println("OS.getCpuEndian() = " + os.getCpuEndian());//
        System.out.println("OS.getDataModel() = " + os.getDataModel());//
        // 系统描述
        System.out.println("OS.getDescription() = " + os.getDescription());
        System.out.println("OS.getMachine() = " + os.getMachine());//
        // 操作系统类型
        System.out.println("OS.getName() = " + os.getName());
        System.out.println("OS.getPatchLevel() = " + os.getPatchLevel());//
        // 操作系统的卖主
        System.out.println("OS.getVendor() = " + os.getVendor());
        // 卖主名称
        System.out.println("OS.getVendorCodeName() = " + os.getVendorCodeName());
        // 操作系统名称
        System.out.println("OS.getVendorName() = " + os.getVendorName());
        // 操作系统卖主类型
        System.out.println("OS.getVendorVersion() = " + os.getVendorVersion());
        // 操作系统的版本号
        System.out.println("OS.getVersion() = " + os.getVersion());
        
        return os;
	}
	
	/**
	 * 取当前系统进程表中的用户信息
	 * @return
	 */
	public Who[] getWhos() {
		Sigar sigar = new Sigar();
		Who who[] = null;
		try {
			who = sigar.getWhoList();
		} catch (SigarException e) {
			e.printStackTrace();
		}
		
		if (who != null && who.length > 0) {
			for (int i = 0; i < who.length; i++) {
				traceln("\n~~~~~~~~~" + String.valueOf(i) + "~~~~~~~~~");
				Who _who = who[i];
				traceln("getDevice() = " + _who.getDevice());
				traceln("getHost() = " + _who.getHost());
				traceln("getTime() = " + _who.getTime());
				//当前系统进程表中的用户名
				traceln("getUser() = " + _who.getUser());
			}
		}
		return who;
	}
}
